package dot;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;

import org.jgrapht.Graph;
import org.jgrapht.ext.VertexNameProvider;

import coloring.MyDefaultEdge;
import coloring.vertex.Vertex;

public class Export {
	/**
	 * @param fileName
	 * @param graph
	 */
	public Export() {
	}

	public void exportToDot(String path, Graph<Vertex, MyDefaultEdge> graph) {
		// DOTExporter<Vertex, DefaultEdge> exporter = new DOTExporter<Vertex,
		// DefaultEdge>();
		DOTExporter<Vertex, MyDefaultEdge> exporter = new DOTExporter<Vertex, MyDefaultEdge>(
				new VertNameProvider<Vertex>(),
				new VertAtributeProvider<Vertex>(), null);

		try {
			FileOutputStream fileOutputStream = new FileOutputStream(path);
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
					fileOutputStream, "UTF-8");
			BufferedWriter out = new BufferedWriter(outputStreamWriter);
			exporter.export(out, graph);
			out.close();
		} catch (IOException e) {
			System.out.println("exception on writing to file");
			e.printStackTrace();
		}
	}

	private class VertNameProvider<V> implements VertexNameProvider<V> {

		@Override
		public String getVertexName(V vertex) {
			Vertex vertex2 = (Vertex) vertex;
			return vertex2.getName();
		}
	}

	private class VertAtributeProvider<V> implements VertexNameProvider<V> {

		@Override
		public String getVertexName(V vertex) {
			Vertex vertex2 = (Vertex) vertex;
			double color1 = 1.0 / vertex2.getColor();
			DecimalFormat format = new DecimalFormat("0.000");
			String presenation1 = format.format(color1);
			presenation1 = presenation1.replace(',', '.');

			double color2 = 3.0 / vertex2.getColor();
			String presenation2 = format.format(color2);
			presenation2 = presenation2.replace(',', '.');

			double color3 = 5.0 / vertex2.getColor();
			String presenation3 = format.format(color3);
			presenation3 = presenation3.replace(',', '.');

			return "[label=\"" + vertex2.getName() + ", c" + vertex2.getColor()
					+ "\", color=\"" + presenation1 + " " + presenation2 + " "
					+ presenation3 + "\"]";
		}
	}

}
